/*
**    This file is the driver code for DAC124S085
**/
#include "DAC124S085.h"
#include "Assistant.h"
#include "FPGA_Register.h"

#if 1
/* added by cjli 20120620 */
void HW_InitDAC124S085(void)
{
    /* set Power down mode: 2.5K to GND */
    //DAC124S085_SetPort(DAC124S085_VOUTA, DAC124S085_OP_POWER_DOWN_OUTPUT, 0);
    //Helper_DelayMs(1000);
    /* set VOUTA */
    DAC124S085_SetPort(DAC124S085_VOUTA, DAC124S085_OP_WRITE_UPDATE, 512);
    Helper_DelayMs(1000);
    /* set VOUTB */
    DAC124S085_SetPort(DAC124S085_VOUTB, DAC124S085_OP_WRITE_UPDATE, 512);
    Helper_DelayMs(1000);
    /* set VOUTC */
    DAC124S085_SetPort(DAC124S085_VOUTC, DAC124S085_OP_WRITE_UPDATE, 500);
    Helper_DelayMs(1000);
    /* set VOUTD */
    DAC124S085_SetPort(DAC124S085_VOUTD, DAC124S085_OP_WRITE_UPDATE, 512);
    Helper_DelayMs(1000);
}

#if 0
void DAC124S085_SetPort(short port, short cmd, short value)
{
    int i;
    DAC_CLKDATA_SEL = 1;
    for(i=0; i<4000; i++)
        Helper_DelayMs(10);    
    DAC5623_CONFIG_START = 0x0;
    //DAC124S085_CONFIG = ((port << 14) | (cmd << 12) | (value & 0xfff));
    //Helper_Delay();
    Helper_DelayMs(100);
    DAC5623_CONFIG_START = 0x1;
    Helper_DelayMs(100);
    DAC_CLKDATA_SEL = 0;
    for(i=0; i<4000; i++)
        Helper_DelayMs(10);
}

#endif

void DAC124S085_SetPort(short port, short cmd, short value)
{
    int i;

    #if 0
    //int i;
    DAC_CLKDATA_SEL = 1;
    for(i=0; i<4000; i++)
        Helper_DelayMs(10);    
    DAC5623_CONFIG_START = 0x0;
    //DAC124S085_CONFIG = ((port << 14) | (cmd << 12) | (value & 0xfff));
    //Helper_Delay();
    Helper_Delay();
    DAC5623_CONFIG_START = 0x1;
    Helper_DelayMs(100);
    DAC_CLKDATA_SEL = 0;
    for(i=0; i<4000; i++)
        Helper_DelayMs(10);
    #else
    
    //DAC_CLKDATA_SEL = 1;
    for(i=0; i<1000; i++)
        Helper_Delay();  
    //if(port == 2)
    //    printf("port = %d, cmd = %d, value = %d\n", port, cmd, value);
    
    DAC_CONFIG_START = (DAC_CONFIG_START) & 0xFFFB;
    DAC_CONFIG = (short)((port << 14) | (cmd << 12) | ((value << 2) & 0xfff));
    Helper_Delay();
    DAC_CONFIG_START = (DAC_CONFIG_START) | 0x4;
    for(i=0; i<1000; i++)
        Helper_Delay();    

    #endif
}


#endif

/* end cjli */

